/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Column-Sorting Comparer Class
 *		An IComparer implementation for the ColumnSortListView control used to sort
 * the list view contents by the content of a specified column.
 *------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// An IComparer implementation for the ColumnSortListView control used to sort
    /// the list view contents by the content of a specified column.
    /// </summary>
    public sealed class ListViewColumnSorter : IComparer
    {
        /*----------------------------------------------------------------------------------------------------
         * Private Member Declarations
         *--------------------------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// The index of the column that was clicked.
        /// </summary>
        private int _columnIndex = 0;
        /// <summary>
        /// The sort direction.
        /// </summary>
        private bool _ascending = true;
        #endregion

        /*----------------------------------------------------------------------------------------------------
		 * Constructor / Dispose Methods
		 *--------------------------------------------------------------------------------------------------*/
        #region Constructor / Dispose Methods
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ListViewColumnSorter()
        {
        }
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <remarks>
        /// Using this constructor defaults the sort direction to ascending.
        /// </remarks>
        /// <param name="column">The index of the column whose contents are to be sorted.</param>
        public ListViewColumnSorter(int column)
        {
            _columnIndex = column;
            _ascending = true;
        }
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <param name="column">The index of the column whose contents are to be sorted.</param>
        /// <param name="asc">
        /// <b>true</b> to sort ascending; <b>false</b> to sort descending.
        /// </param>
        public ListViewColumnSorter(int column, bool asc)
        {
            _columnIndex = column;
            _ascending = asc;
        }
        #endregion

        /*----------------------------------------------------------------------------------------------------
		 * Public Methods / Functions
		 *--------------------------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, 
        /// equal to or greater than the other.
        /// </summary>
        /// <param name="leftItem">
        /// First object to compare.
        /// </param>
        /// <param name="rightItem">
        /// Second object to compare.
        /// </param>
        /// <returns>
        /// Less than zero if leftItem is less than rightItem,
        /// Zero if leftItem equals rightItem, and
        /// Greater than zer if leftItem is greater than rightItem.
        /// </returns>
        public int Compare(object leftItem, object rightItem)
        {
            ListViewItem leftInstance = null;
            ListViewItem rightInstance = null;
            string leftText = string.Empty;		//The text in the left column.
            string rightText = string.Empty;	//The text in the right column.
            int returnValue = 0;				//Return value.

            //Get the actual text from the list view.
            leftInstance = (ListViewItem)leftItem;
            rightInstance = (ListViewItem)rightItem;

            if ((leftInstance != null) && (leftInstance.SubItems.Count > _columnIndex))
                leftText = leftInstance.SubItems[_columnIndex].Text;

            if ((rightInstance != null) && (rightInstance.SubItems.Count > _columnIndex))
                rightText = rightInstance.SubItems[_columnIndex].Text;

            //Compare and return the result.
            returnValue = leftText.CompareTo(rightText);

            //If sort order is descending, invert the result.
            if (!_ascending)
                returnValue *= -1;

            leftText = null;
            rightText = null;

            return returnValue;
        }
        #endregion
    }
}
